{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "6453d3d5",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/llm/octoai.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "72ed6f61-28a7-4f90-8a45-e3f452f95dbd",
   "metadata": {},
   "source": [
    "# OctoAI "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c78b172f",
   "metadata": {},
   "source": [
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b4d1be6",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-llms-octoai\n",
    "%pip install llama-index\n",
    "%pip install octoai-sdk"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f8ec3ad",
   "metadata": {},
   "source": [
    "Include your OctoAI API key below. You can get yours at [OctoAI](https://octo.ai). \n",
    "\n",
    "[Here](https://octo.ai/docs/getting-started/how-to-create-an-octoai-access-token) are some instructions in case you need more guidance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8249662",
   "metadata": {},
   "outputs": [],
   "source": [
    "OCTOAI_API_KEY = \"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f87c3024",
   "metadata": {},
   "source": [
    "#### Initialize the Integration with the default model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e4919848",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.llms.octoai import OctoAI\n",
    "\n",
    "octoai = OctoAI(token=OCTOAI_API_KEY)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b81a3ef6-2ee5-460d-9aa4-f73708774014",
   "metadata": {},
   "source": [
    "#### Call `complete` with a prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "910b50ad-c55e-487e-8808-5905dfaa78b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = octoai.complete(\"Paul Graham is \")\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6ad0a98-92dd-48fd-9823-175d701c1ab2",
   "metadata": {},
   "source": [
    "#### Call `chat` with a list of messages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5fd3137b-05ce-40a5-bdb0-5ce048f5ca25",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.llms import ChatMessage\n",
    "\n",
    "messages = [\n",
    "    ChatMessage(\n",
    "        role=\"system\",\n",
    "        content=\"Below is an instruction that describes a task. Write a response that appropriately completes the request.\",\n",
    "    ),\n",
    "    ChatMessage(role=\"user\", content=\"Write a blog about Seattle\"),\n",
    "]\n",
    "response = octoai.chat(messages)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56a55ce6-08e3-4534-9bae-345686308b3e",
   "metadata": {},
   "source": [
    "## Streaming"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57901d1c-d1d4-442e-bb91-cd8f054ae2fd",
   "metadata": {},
   "source": [
    "Using `stream_complete` endpoint "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd9e2b22-7e62-4f50-a9af-84453aeda071",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = octoai.stream_complete(\"Paul Graham is \")\n",
    "for r in response:\n",
    "    print(r.delta, end=\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19b226da",
   "metadata": {},
   "source": [
    "Using `stream_chat` with a list of messages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10b63238-8d01-48f7-b2ec-a56d23fec172",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.llms import ChatMessage\n",
    "\n",
    "messages = [\n",
    "    ChatMessage(\n",
    "        role=\"system\",\n",
    "        content=\"Below is an instruction that describes a task. Write a response that appropriately completes the request.\",\n",
    "    ),\n",
    "    ChatMessage(role=\"user\", content=\"Write a blog about Seattle\"),\n",
    "]\n",
    "response = octoai.stream_chat(messages)\n",
    "for r in response:\n",
    "    print(r.delta, end=\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4b6ea50-d777-4174-a326-6e4e57b9ea8b",
   "metadata": {},
   "source": [
    "## Configure Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ce3de8d-287e-402d-936f-64a106c8fac2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# To customize your API token, do this\n",
    "# otherwise it will lookup OCTOAI_TOKEN from your env variable\n",
    "octoai = OctoAI(\n",
    "    model=\"mistral-7b-instruct\", max_tokens=128, token=OCTOAI_API_KEY\n",
    ")\n",
    "\n",
    "response = octoai.complete(\"Paul Graham is \")\n",
    "print(response)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  },
  "vscode": {
   "interpreter": {
    "hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
